#!/usr/bin/env python
##########################################################################
#  Copyright (C) 2007 Robert Connolly <rob_connolly@mailshack.com>       #
#                                                                        #
#  This program is free software; you can redistribute it and/or modify  #
#  it under the terms of the GNU General Public License as published by  #
#  the Free Software Foundation; either version 3 of the License, or     #
#  (at your option) any later version. See file COPYING for details.     #
#                                                                        #
#  This program is distributed in the hope that it will be useful,       #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#  GNU General Public License for more details.                          #
#                                                                        #
#  You should have received a copy of the GNU General Public License     #
#  along with this program; if not, write to the                         #
#  Free Software Foundation, Inc.,                                       #
#  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
##########################################################################

import dbus, dbus.glib
import sys, os
import time

help_text = """
Usage: ./cairolets <operation> [display]

Summary of command line options:

--help | -h - displays this message

--add | -a [display] - adds the desklet with display name [display] to the daemon 
			(starting it if not already running)

--remove | -r [display] - removes the specified desklet from the daemon

--kill | -k - kills the daemon, saving any running desklets

--list | -l - lists currently running desklets
"""

def Main(args):

	try:
		if args[1] == "--help":
			print help_text
			return 0
		elif args[1] == "-h":
			print help_text
			return 0
	except IndexError:
		pass
		
	bus = dbus.SessionBus()
	
	daemon = None
	tries = 0

	while daemon == None:
		try:
			daemon = dbus.Interface(bus.get_object('com.googlecode.cairolets', '/Daemon'), 'com.googlecode.cairolets.Daemon')
		except dbus.exceptions.DBusException:
			if tries == 5:
				return "ERROR: Unable to start cairolets-daemon!"
			os.spawnvp(os.P_NOWAIT,"./cairolets-daemon", [])
			time.sleep(2)
			tries += 1
	
	try:
		if args[1] == "--kill-daemon":
			daemon.kill()
			return 0
		elif args[1] == "-k":
			daemon.kill()
			return 0
			
		elif args[1] == "--add":
			ret = daemon.add_desklet(args[2])
			if ret['status'] == False:
				return "ERROR: %s" % ret['message']
		elif args[1] == "-a":
			ret = daemon.add_desklet(args[2])
			if ret['status'] == False:
				return "ERROR: %s" % ret['message']
				
		elif args[1] == "--remove":
			ret = daemon.remove_desklet(args[2])
			if ret['status'] == False:
				return "ERROR: %s" % ret['message']
		elif args[1] == "-r":
			ret = daemon.remove_desklet(args[2])
			if ret['status'] == False:
				return "ERROR: %s" % ret['message']
				
		elif args[1] == "--list":
			lst = daemon.list_desklets()
			if len(lst) == 0:
				return "No running desklets!"
			for l in lst:
				print l
				return 0
		elif args[1] == "-l":
			lst = daemon.list_desklets()
			if len(lst) == 0:
				return "No running desklets!"
			for l in lst:
				print l
				return 0
				
	except IndexError:
		return "Usage: %s <operation> [display]" % args[0]

if __name__ == "__main__":
	sys.exit(Main(sys.argv))

